<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Hdcustomermodel extends CI_Model {

    protected $_CI;

    public static $searchField = array(
        'customer_status' => 'customer_status',
        'sex' => 'sex',
        'customer_time_create_start' => 'customer_time_create >=',
        'customer_time_create_end' => 'customer_time_create <=',
        'openid' => 'openid',
        'gift_type' => 'hd_game_customer.gift_type',
        'customer_id' => 'hd_game_customer.customer_id',
    );

    public static $likeField = array(
        'nickname' => 'nickname',
        'province' => 'province',
        'city' => 'city',
        'customer_phone' => 'customer_phone',
    );
    
    public static $sortField = array(
        'lottery_times' => 'lottery_times',
        'money' => 'money',
        'gift_type' => 'hd_game_customer.gift_type',
    );

    public static $historySearchField = array(
        'customer_id' => 'hd_game_lottery_history.customer_id',
        'relonline_customer_id' => 'relonline_customer_id',
        'gift_type' => 'hd_game_lottery_history.gift_type',
    );

    public static $historyLikeField = array(
        'nickname' => 'nickname',
    );

    public function __construct(){
        parent::__construct();
        $this->_CI = & get_instance();
    }
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
    public function getCustomerListSection($assist){
        if(!empty($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('hd_game_customer');
        if(isset($assist['like']['nickname'])) {
            $assist['like']['nickname'] = urlencode($assist['like']['nickname']);
        }
        if(!empty($assist['where'])) $this->db->where($assist['where']);
        if(!empty($assist['like'])) $this->db->like($assist['like']);
        if(!empty($assist['where_in'])){
            foreach ($assist['where_in'] as $k => $v) {
                if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
                else {
                    $query = $this->db->get();
                    return array(array(),0);
                }
            }
        }
        if(!empty($assist['pagination'])) {
            $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);
        }

        if(!empty($assist['sort'])) $this->db->order_by($assist['sort']['key'],$assist['sort']['dir']);
        $this->db->order_by('hd_game_customer.customer_id','desc');
        if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        if(empty($assist['pagination'])) {
            $count = count($list);
        }

        return array($list,$count);
    }


    public function getCustomerById($customer_id){
        if(!is_array($customer_id)) $customer_id = array($customer_id);
        if(empty($customer_id)) return array();

        $this->db->from('hd_game_customer');
        $this->db->where_in('hd_game_customer.customer_id',$customer_id);
        $query = $this->db->get();
        $list = $query->result_array();

        return $list;
    }


    public function getCustomer($customer_id){
        $this->db->from('hd_game_customer');
        $this->db->where('hd_game_customer.customer_id',$customer_id);
        $this->db->limit(1);
        $query = $this->db->get();
        $customer = $query->row_array();

        return $customer;
    }


    public function getCustomerByOpenid($openid,$select = ''){
        if (!empty($select)) $this->db->select($select);
        $this->db->from('hd_game_customer');
        $this->db->where('openid',$openid);
        $this->db->limit(1);
        $query = $this->db->get();
        $customer = $query->row_array();

        return $customer;
    }

    public function getCustomerByRelonlineCustomerId($relonline_customer_id,$select = ''){
        if (!empty($select)) $this->db->select($select);
        $this->db->from('hd_game_customer');
        $this->db->where('relonline_customer_id',$relonline_customer_id);
        $this->db->limit(1);
        $query = $this->db->get();
        $customer = $query->row_array();

        return $customer;
    }

    

    public function getLotteryHistoryListSection($assist){
        if(isset($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('hd_game_lottery_history');
        $this->db->join('hd_game_customer','hd_game_customer.customer_id = hd_game_lottery_history.customer_id','left');

        if(isset($assist['like']['nickname'])) {
            $assist['like']['nickname'] = urlencode($assist['like']['nickname']);
        }
        if(isset($assist['where'])) $this->db->where($assist['where']);
        if(isset($assist['like'])) $this->db->like($assist['like']);
        if (isset($assist['where_in']) && !empty($assist['where_in'])) {
            foreach ($assist['where_in'] as $k => $v) {
                if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
                else {
                    $query = $this->db->get();
                    if(isset($assist['nocount'])) return array();
                    return array(array(),0);
                }
            }
        }

		if(!empty($assist['pagination'])) $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

        if(!empty($assist['sort'])) $this->db->order_by($assist['sort']['key'],$assist['sort']['dir']);
        $this->db->order_by('lottery_history_id','desc');
        if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        if(empty($assist['pagination'])) $count = count($list);

        return array($list,$count);
    }

    public function getTotalGameCount() {
        $this->db->from('hd_game_history');
        $count = $this->db->count_all_results();
        return $count;
    }

    public function getLinearWinGameCount($score) {
        $this->db->from('hd_game_history');
        if($score > 325) {
            $this->db->where('game_score >',325 - ($score - 325));
            $this->db->where('game_score <', $score);
        } else {
            $this->db->where('game_score >',$score);
            $this->db->where('game_score <', 325 + (325 - $score));
        }
        $count = $this->db->count_all_results();
        return $count;
    }

    public function getLastLotteryTime($customer_id) {
        $this->db->from('hd_game_lottery_history');
        $this->db->where('customer_id',$customer_id);
        $this->db->order_by('lottery_history_id','DESC');
        $this->db->limit(1);
        $query = $this->db->get();
        $result = $query->row_array();
        $time = 0;
        if(!empty($result)) {
            $time = strtotime($result['lottery_time_time_lastmodified']);
        }

        return $time;
    }

/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
    public function createCustomer($info){
        $db_debug = $this->db->db_debug;
        // $this->db->db_debug = FALSE; 
        $m = $this->db->insert('hd_game_customer',$info);
        $customer_id = $this->db->insert_id();
        $this->db->db_debug = $db_debug;

        return $m ? $customer_id : 0;
    }

    public function createLotteryHistory($info) {
        $this->db->insert('hd_game_lottery_history',$info);
        $id = $this->db->insert_id();
        return $id;
    }

    public function createHistory($info) {
        $this->db->insert('hd_game_history',$info);
        $id = $this->db->insert_id();
        return $id;
    }

/*
| -------------------------------------------------------------------
|  EDIT Functions
| -------------------------------------------------------------------
*/
    public function editCustomer($info) {
        $this->db->where('customer_id',$info['customer_id']);
        unset($info['customer_id']);
        $this->db->update('hd_game_customer',$info);
    }

    public function updateCustomerLotteryCount($customer_id) {
        $this->db->query('update {PRE}hd_game_customer a set lottery_times=(select count(*) 
                from {PRE}hd_game_lottery_history where customer_id=a.customer_id) WHERE a.customer_id='.$customer_id);
    }
/*
| -------------------------------------------------------------------
|  CHECK Functions
| -------------------------------------------------------------------
*/
	public function checkHasLotteryGift($customer_id){
        $this->db->from('hd_game_lottery_history');
        $this->db->where('customer_id',$customer_id);
        $this->db->where('gift_type >',0);
        $count = $this->db->count_all_results();

        return ($count > 0);
    }

}